home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / lib / dial / d_io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-02-01  |  2.7 KB  |  141 lines

  1. #
  2. #include    <stdio.h>
  3.  
  4. /*  This is a set of routines that is called to read characters from
  5.  *  the tty line.  It stores them up so that they may be "replayed"
  6.  *  if necessary.  The characters are stored in a circular buffer.
  7. */
  8.  
  9. /*  The following functions are implemented:
  10.  *
  11.  *    getc - returns 1 character from the appropriate place.
  12.  *    mark - designates the next character to be read as the one
  13.  *           to start with if a replay is requested.
  14.  *    replay - further calls to d_getc() will read characters from
  15.  *         the buffer until it is exhausted.
  16.  *
  17.  *
  18.  *    Added the select call to adapt to 4.2's signal mechanism.
  19.  *        DRockwell@BBN 31 Jan 84
  20.  *    Backed out above change in favor of the s_io library.
  21.  *        DRockwell@BBN 1 Mar 84
  22. */
  23.  
  24. #define        BUFFSIZE        256
  25.  
  26. static int nleft;    /*  number of chars remaining in this replay    */
  27. static int current;    /*  index of current char in this replay    */
  28. static int last;    /*  next position to insert character at    */
  29. static int nchars;    /*  total number of chars in buffer.  Note that */
  30.             /*  this stays the same throughout a replay.    */
  31. static int first;    /*  index of the first character in the buffer  */
  32. static int replay;    /*  set if we are doing a replay        */
  33.  
  34. #ifdef V4_2BSD
  35. static int rbuff[BUFFSIZE];
  36. #else
  37. static char rbuff[BUFFSIZE];
  38. #endif
  39.  
  40. d_getc (channel)
  41. register FILE *channel;
  42. {
  43. #ifdef V4_2BSD
  44.     register int c;
  45. #else
  46.     char c;
  47. #endif
  48.  
  49.  
  50.     /*  Check to see if we are replaying the input  */
  51.     if (replay != 0)
  52.     {
  53.     if (nleft == 0)
  54.     {
  55.         replay = 0;
  56.         return (d_getc (channel));
  57.     }
  58.  
  59.     c = rbuff[current];
  60.     current = indexinc (current);
  61.     nleft--;
  62.  
  63.     /*  Be careful not to return an EOF from the queue.  They
  64.      *  get in here when an alarm call from the timeout code
  65.      *  interrupts a read.  However, they should clearly not
  66.      *  be returned from the queue, as they may not represent
  67.      *  the current state of things.
  68.      */
  69.     if (c == EOF)
  70.         return (d_getc (channel));
  71.     }
  72.     else
  73.     {
  74.     if (nchars == BUFFSIZE)
  75.         /*  Have run out of buffer space.  Rather then leave
  76.          *  a partial line, which could screw up 'rdport',
  77.          *  delete the first line in the buffer
  78.          */
  79.         skpline();
  80.     else
  81.         nchars++;
  82.     c = getc (channel);
  83.  
  84.     rbuff[last] = c;
  85.     last = indexinc (last);
  86.     }
  87.  
  88.     return (c);
  89. }
  90.  
  91.  
  92. d_replay ()
  93.     {
  94.  
  95.     replay = 1;
  96.     current = first;
  97.     nleft = nchars;
  98.  
  99. }
  100.  
  101.  
  102. d_mark ()
  103.     {
  104.  
  105.     replay = 0;
  106.     nchars = 0;
  107.     first = 0;
  108.     last = 0;
  109. }
  110.  
  111.  
  112. static indexinc (index)
  113.   int index;
  114.     {
  115.  
  116.     if (index == (BUFFSIZE-1))
  117.     return (0);
  118.     else
  119.     return (index + 1);
  120. }
  121.  
  122.  
  123. static skpline ()
  124.     {
  125.  
  126.     while ((rbuff[first] != '\n') &&
  127.        (nchars > 0          )   )
  128.     {
  129.     first++;
  130.     nchars--;
  131.     }
  132.  
  133.     if (nchars <= 0)
  134.     d_mark ();
  135.     else
  136.     {
  137.     first++;
  138.     nchars--;
  139.     }
  140. }
  141.